﻿using Ext.Net;
using Ext.Net.MVC;
using PcaCore.DAL.Log;
using PcaParqueos.BOL;
using PcaParqueos.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PcaWeb.Areas.Parqueos.Controllers
{
    [DirectController(AreaName = "Parqueos")]
    public class VehiculoAbonadoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList(StoreRequestParameters parameters, string filtro)
        {
            int total;
            List<oVwVehiculoAbonado> vehiculos = dVehiculoAbonado.GetList(parameters.Start,
                                                                          parameters.Limit,
                                                                          out total,
                                                                          dSesion.GetEmpresaId(),
                                                                          dSesion.GetOficinaId(),  
                                                                          filtro);
            return this.Store(vehiculos, total);
        }

        public ActionResult FrmRegistrar()
        {
            return new Ext.Net.MVC.PartialViewResult
            {
                ViewName = "FrmRegistrar"
            };
        }

        private ActionResult ConfirmarEliminar(oVwVehiculoAbonado vehiculo)
        {
            try
            {
                MessageBoxConfig config = new MessageBoxConfig();
                config.Title = "Confirmacion";
                config.Message = string.Format("Eliminar vehiculo Placa: \"{0}\" Abonado: \"{1}\"",
                                               vehiculo.placa,
                                               vehiculo.cliente);
                config.Closable = false;
                config.Icon = MessageBox.Icon.QUESTION;
                config.Buttons = MessageBox.Button.YESNO;
                config.MessageBoxButtonsConfig = new MessageBoxButtonsConfig
                {
                    Yes = new MessageBoxButtonConfig { Handler = string.Format("VehiculoAbonado.Parqueos.Eliminar(\"{0}\")", vehiculo.vehiculo_id), Text = "Si" },
                    No = new MessageBoxButtonConfig { Text = "No" }
                };

                X.Msg.Show(config);
                return this.Direct();
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }

        [DirectMethod(Namespace = "VehiculoAbonado")]
        public ActionResult Eliminar(long? vehiculoId)
        {
            try
            {
                dVehiculoAbonado.Delete(dSesion.GetEmpresaId(), dSesion.GetOficinaId(), Convert.ToInt64(vehiculoId));  
                this.GetCmp<Store>("storeVehiculosAbonados").LoadProxy();
                return this.Direct();
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }

        [HttpPost]
        public ActionResult Registrar(int? tipoVehiculoId, string placa, long? clienteId)
        {

            try
            {
                dVehiculoAbonado.Insert(dSesion.GetEmpresaId(),
                                        dSesion.GetOficinaId(),
                                        Convert.ToInt32(tipoVehiculoId),
                                        placa,
                                        Convert.ToInt64(clienteId),
                                        dSesion.GetId());  
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }

        public ActionResult RunCommandGrid(string commandName, long? vehiculoId)
        {
            oVwVehiculoAbonado vehiculo = dVehiculoAbonado.Get(dSesion.GetEmpresaId(), dSesion.GetOficinaId(), Convert.ToInt64(vehiculoId));

            if (vehiculo == null)
                return Json(new { success = false, msg = "El registro ya no existe en la base de datos, posiblemente fue eliminado por otro usuario" });

            switch (commandName)
            {
                case "cmdEliminar":
                    return ConfirmarEliminar(vehiculo);
                default:
                    return Json(new { success = false, msg = String.Format("\"{0}\" no es un comando valido", commandName) });
            }
        }
    }
}